﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmQLNhaCungCap : Form
    {
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private TaiKhoanMgt taikhoanMgt = new TaiKhoanMgt();
        private bool IsEdited = false;

        #region Xu ly Load
        public FrmQLNhaCungCap()
        {
            InitializeComponent();
        }

        private void FrmQLNhaCungCap_Load(object sender, EventArgs e)
        {
            lblID.Hide();
            ClearTextBox();
            groupBox1.Enabled = false;
            BindingToGrid();
            btnLuu.Enabled = false;
            btnDelete.Enabled = false;
            btnUpdate.Enabled = false;
        }

        public void BindingToGrid()
        {
            ResultObj result = new ResultObj();
            result = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> lstNhaCungCap = (List<NhaCungCap>)result.Result;
            dgvNhaCungCap.AutoGenerateColumns = false;
            dgvNhaCungCap.DataSource = lstNhaCungCap;
        }
        #endregion

        #region Xu ly cac Button
        private void btnThem_Click(object sender, EventArgs e)
        {
            ClearTextBox();
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);

            IsEdited = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnLuu.Enabled = true;
            txtTenNhaCC.Select();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            IsEdited = true;
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);
            btnLuu.Enabled = true;
            txtTenNhaCC.Select();

            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            if (txtTenNhaCC.Text == "" || txtDiaChi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin !", "Thông báo");
            }
            else
            {
                ResultObj result = new ResultObj();

                string sTen = txtTenNhaCC.Text;
                string sDiaChi = txtDiaChi.Text;
                string sDienThoai = txtDienThoai.Text;
                string sMaSoThue = txtMaSoThue.Text;

                NhaCungCap nhaCungCap = new NhaCungCap();

                nhaCungCap.TenNhaCungCap = sTen;
                nhaCungCap.DiaChi = sDiaChi;
                nhaCungCap.DienThoai = sDienThoai;
                nhaCungCap.MaSoThue = sMaSoThue;

                if (!IsEdited)
                {
                    result = nhacungcapMgt.SaveNewNhaCungCap(nhaCungCap);
                }
                else
                {
                    nhaCungCap.Id = Convert.ToInt32(lblID.Text);
                    result = nhacungcapMgt.UpdateNhaCungCap(nhaCungCap);
                    IsEdited = false;
                }

                if (result.ReturnCode > 0)
                {
                    MessageBox.Show(" Lưu thành công", "Thông báo");
                    FrmQLNhaCungCap_Load(null, null);
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    txtTenNhaCC.Select();
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    txtTenNhaCC.Select();
                }

                BindingToGrid();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Bạn có chắc chắn muốn xóa nhà cung cấp này không?", "Xóa nhà cung cấp", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                ResultObj resultObj = new ResultObj();

                string sTen = txtTenNhaCC.Text;
                string sDiaChi = txtDiaChi.Text;
                string sDienThoai = txtDienThoai.Text;
                string sMaSoThue = txtMaSoThue.Text;

                NhaCungCap nhaCungCap = new NhaCungCap();

                nhaCungCap.TenNhaCungCap = sTen;
                nhaCungCap.DiaChi = sDiaChi;
                nhaCungCap.DienThoai = sDienThoai;
                nhaCungCap.MaSoThue = sMaSoThue;
                nhaCungCap.Id = Convert.ToInt32(lblID.Text);
                nhaCungCap.IsDelete = 1;

                resultObj = nhacungcapMgt.UpdateNhaCungCap(nhaCungCap);

                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
                btnLuu.Enabled = true;

                BindingToGrid();

                ClearTextBox();
                groupBox1.Enabled = true;
                ChangeStatusTextBox(false);
            }
        }
        #endregion

        #region Xu ly cac su kien
        public void ClearTextBox()
        {
            txtTenNhaCC.Clear();
            txtDiaChi.Clear();
            txtDienThoai.Clear();
            txtMaSoThue.Clear();
        }

        public void ChangeStatusTextBox(bool IsReadOnly)
        {
            if (IsReadOnly)
            {
                txtTenNhaCC.ReadOnly = true;
                txtDiaChi.ReadOnly = true;
                txtMaSoThue.ReadOnly = true;
                txtDienThoai.ReadOnly = true;
            }
            else
            {
                txtTenNhaCC.ReadOnly = false;
                txtDiaChi.ReadOnly = false;
                txtMaSoThue.ReadOnly = false;
                txtDienThoai.ReadOnly = false;
            }
        }

        private void dgvNhaCungCap_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                ChangeStatusTextBox(true);
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
                btnLuu.Enabled = false;

                lblID.Text = dgvNhaCungCap.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtTenNhaCC.Text = dgvNhaCungCap.Rows[e.RowIndex].Cells[1].Value.ToString();
                txtDiaChi.Text = dgvNhaCungCap.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtDienThoai.Text = dgvNhaCungCap.Rows[e.RowIndex].Cells[3].Value.ToString();
                txtMaSoThue.Text = dgvNhaCungCap.Rows[e.RowIndex].Cells[4].Value.ToString();
            }
        }

        private void txtDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }
        #endregion        
    }
}
